package com.order.service;

import com.common.service.AbstractTypedService;
import com.order.dao.OrdersDao;
import com.order.entities.Orders;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author fu
 */
@Component
public class OrderServiceImpl extends AbstractTypedService<Orders, Integer> {
    OrdersDao ordersDao;

    OrderServiceImpl( OrdersDao dao ) {
        this.ordersDao = dao;
        this.dataContext = dao;
    }

    public Orders order( Integer uid, Integer bid, double totalprice ) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Orders order = Orders.builder()
                .userId(uid)
                .businessId(bid)
                .orderState(0)
                .orderTotal(totalprice)
//                .daId(deliveryAddressDao.findByUserId(uid).getId())
                .orderDate(dateFormat.format(date))
                .build();
        return ordersDao.save(order);
    }

    public Boolean pay(Integer orderId){
        Orders orders=ordersDao.getById(orderId);
        orders.setOrderState(1);
        return ordersDao.save(orders).getOrderState() == 1;
    }

    public List<Orders> findBybid(Integer bid) {
        return ordersDao.findOrdersByBusinessId(bid);
    }

    public List<Orders> findByuid(Integer uid) {
        return ordersDao.findOrdersByUserId(uid);
    }
}
